package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.AZBusinessAreaAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.ztesoft.bss.base.util.LocalAttrUtils;
import com.ztesoft.bss.common.util.SpringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Classname LocalInstallAddressAttrLinkHandler
 * @Description 获取 省市县 组合组件的值，逗号分隔的字符串，分隔后回填到 省、市、县 对应的各个属性中去
 * @Author zhang.song
 * @Date 2021-03-02 14:53
 */
@Service
@AttrHandler(AZBusinessAreaAttrHandler.class)
public class AZBusinessAreaAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Autowired
    private IQuerySceneInstService querySceneInstService;
    @Autowired
    private IOperateSceneInstService operateSceneInstService;
    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        // 场景初始化过程中不校验
        String isInitPageRenderFlag = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "isInitPageRenderFlag");
        if (null!=isInitPageRenderFlag&&KiteStringUtils.isEqual(isInitPageRenderFlag, LocalKeyConsts.IFTRUE_T)) {
            return;
        }
        if(Objects.nonNull(attr.getValue()) && KiteStringUtils.equals(attr.getAttrValueTypeId(),"98A") && attr.getAttrValues().size()>0) {
            KiteAttrValue value = attr.getAttrValues().stream().filter(attrValue -> attr.getValue().equals(attrValue.getAttrValue())).findFirst().get();
            //处理县分营业区编码、网格联动
            if (KiteStringUtils.equalsAny(attr.getFieldName(), "2_0000132")) {
                KiteAttr attr_210008155 = inst.getAttr("210008155");
                //网格
                KiteAttr attr_210008156 = inst.getAttr("210008156");
                attr_210008155.setValue(value.getAttrValueDesc());
                attr_210008155.setValueDesc(value.getAttrValueDesc());
                link(value.getAttrValueDesc(), attr_210008156,inst,data.getSceneInstId());
                changeScene(attr,attr_210008156,data.getSceneInstId(),inst);
                operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), attr_210008155);
                operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), attr_210008156);
            } else if (KiteStringUtils.equalsAny(attr.getFieldName(), "2_0000133")) {
                KiteAttr attr_210008157 = inst.getAttr("210008157");
                //网格
                KiteAttr attr_210008158 = inst.getAttr("210008158");
                attr_210008157.setValue(value.getAttrValueDesc());
                attr_210008157.setValueDesc(value.getAttrValueDesc());
                link(value.getAttrValueDesc(), attr_210008158,inst,data.getSceneInstId());
                changeScene(attr,attr_210008158,data.getSceneInstId(),inst);
                operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), attr_210008157);
                operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), attr_210008158);
            } else if (KiteStringUtils.equalsAny(attr.getFieldName(), "210008153")) {
                KiteAttr attr_200003327 = inst.getAttr("200003327");
                KiteAttr attr_200001378 = inst.getAttr("200001378");
//                网格
                attr_200003327.setValue(value.getAttrValueDesc());
                attr_200003327.setValueDesc(value.getAttrValueDesc());
                link(value.getAttrValueDesc(), attr_200001378,inst,data.getSceneInstId());
                changeScene(attr,attr_200001378,data.getSceneInstId(),inst);
                operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), attr_200003327);
                operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), attr_200001378);
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
    private void link(String attrValue,KiteAttr linked,KiteInst inst,String sceneInstId ){
        linked.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_M);
        JSONObject org= LocalAttrUtils.getInstance().queryGovEnterpriseOrg("2",attrValue);
        JSONArray orgListJson = org.getJSONArray("ORG_LIST");
        List<KiteAttrValue> attrValues = new ArrayList<>();
        attrValues.add(LocalAttrUtils.pleaseSelect());
        if(!(orgListJson.isEmpty()|| orgListJson.size()<1)){
            List<JSONObject> values =  org.getJSONArray("ORG_LIST").toJavaList(JSONObject.class);
            String level = org.getString("OUCLASS_CODE");
            if (null!=linked&&values.size()>0) {
                for (JSONObject jo : values) {
                    KiteAttrValue kiteAttrValue = new KiteAttrValue();
                    kiteAttrValue.setAttrValue(jo.getString("ORG_NAME"));
                    kiteAttrValue.setAttrValueName(jo.getString("ORG_NAME"));
                    kiteAttrValue.setAttrValueDesc(jo.getString("ORG_CODE"));
                    kiteAttrValue.setAttrId(LocalAttrUtils.pleaseSelect().getAttrId());
                    kiteAttrValue.setSortBy("1");
                    attrValues.add(kiteAttrValue);
                }
            }
        }
        linked.setAttrValues(attrValues);
    }
    private void changeScene(KiteAttr attr,KiteAttr linked,String sceneInstId,KiteInst inst){
        //        业务变更初始化
        String BUSI_CHANGE_SCENE_ONE = querySceneInstService.getExtParameterValue(sceneInstId, LocalKeyConsts.BUSI_CHANGE_SCENE_ONE);
        if (null!=BUSI_CHANGE_SCENE_ONE&&KiteStringUtils.isEqual(BUSI_CHANGE_SCENE_ONE, LocalKeyConsts.IFTRUE_T)) {
            return;
        }
        String BUSI_CHANGE = querySceneInstService.getExtParameterValue(sceneInstId, LocalKeyConsts.BUSI_CHANGE);
        //极简受理-标记是否已进入业务变更场景：T-业务变更，其他情况(F,或者空)-其他业务场景
        if (null!=BUSI_CHANGE&&KiteStringUtils.isEqual(BUSI_CHANGE, LocalKeyConsts.IFTRUE_T)) {
            if(!attr.getOriginValue().equals(attr.getValue())){
                linked.setValue(StringUtils.EMPTY);
                linked.setValueDesc(StringUtils.EMPTY);
                if(null==linked.getValue()){
                    if (KiteStringUtils.equalsAny(linked.getFieldName(), "200001378", "200002595")) {
                        KiteAttr attr_200002594 = inst.getAttr("200002594");
                        attr_200002594.setValue(StringUtils.EMPTY);
                        attr_200002594.setValueDesc(StringUtils.EMPTY);
                        operateSceneInstService.setAttr(sceneInstId, inst.getInstType(), inst.getInstId(), attr_200002594);
                    } else if (KiteStringUtils.equalsAny(linked.getFieldName(), "210008156", "200002603")) {
                        KiteAttr attr_200002602 = inst.getAttr("200002602");
                        attr_200002602.setValue(StringUtils.EMPTY);
                        attr_200002602.setValueDesc(StringUtils.EMPTY);
                        operateSceneInstService.setAttr(sceneInstId, inst.getInstType(), inst.getInstId(), attr_200002602);
                    } else if (KiteStringUtils.equalsAny(linked.getFieldName(), "210008158", "200002605")) {
                        KiteAttr attr_200002604 = inst.getAttr("200002604");
                        attr_200002604.setValue(StringUtils.EMPTY);
                        attr_200002604.setValueDesc(StringUtils.EMPTY);
                        operateSceneInstService.setAttr(sceneInstId, inst.getInstType(), inst.getInstId(), attr_200002604);
                    }
                }
            }
        }else{
            if(null!=attr.getValue()&&!attr.getValue().equals(attr.getOldValue())){
                linked.setValue(StringUtils.EMPTY);
                linked.setValueDesc(StringUtils.EMPTY);
                if(null==linked.getValue()){
                    if (KiteStringUtils.equalsAny(linked.getFieldName(), "200001378", "200002595")) {
                        KiteAttr attr_200002594 = inst.getAttr("200002594");
                        attr_200002594.setValue(StringUtils.EMPTY);
                        attr_200002594.setValueDesc(StringUtils.EMPTY);
                        operateSceneInstService.setAttr(sceneInstId, inst.getInstType(), inst.getInstId(), attr_200002594);
                    } else if (KiteStringUtils.equalsAny(linked.getFieldName(), "210008156", "200002603")) {
                        KiteAttr attr_200002602 = inst.getAttr("200002602");
                        attr_200002602.setValue(StringUtils.EMPTY);
                        attr_200002602.setValueDesc(StringUtils.EMPTY);
                        operateSceneInstService.setAttr(sceneInstId, inst.getInstType(), inst.getInstId(), attr_200002602);
                    } else if (KiteStringUtils.equalsAny(linked.getFieldName(), "210008158", "200002605")) {
                        KiteAttr attr_200002604 = inst.getAttr("200002604");
                        attr_200002604.setValue(StringUtils.EMPTY);
                        attr_200002604.setValueDesc(StringUtils.EMPTY);
                        operateSceneInstService.setAttr(sceneInstId, inst.getInstType(), inst.getInstId(), attr_200002604);
                    }
                }
            }
        }

    }
}
